package com.weixing.mall.provider.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.weixing.mall.base.utils.EnumUtil;
import com.weixing.mall.provider.api.model.enums.ProfitFromEnum;
import com.weixing.mall.provider.mapper.AgentBonusRuleMapper;
import com.weixing.mall.provider.model.domain.AgentBonusRule;
import com.weixing.mall.provider.model.dto.AgentBonusRuleDto;
import com.weixing.mall.provider.api.model.vo.AgentBonusRuleVo;
import com.weixing.mall.provider.service.IAgentBonusRuleService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author wx
 * @Date 2019/9/23 14:55
 * @Version 1.0
 */

/**
 * <p>
 * 代理商模块-代理商分红规则配置表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-18
 */
@Service
public class AgentBonusRuleServiceImpl extends ServiceImpl<AgentBonusRuleMapper, AgentBonusRule> implements IAgentBonusRuleService {

    @Override
    public void save(AgentBonusRuleDto agentBonusRuleDto) {
        AgentBonusRule agentBonusRule = new AgentBonusRule();
        BeanUtils.copyProperties(agentBonusRuleDto, agentBonusRule);
        agentBonusRule.setOrderType(agentBonusRuleDto.getOrderType().getValue());
        saveOrUpdate(agentBonusRule);
    }

    @Override
    public List<AgentBonusRuleVo> selectListOfPage() {
        ArrayList<AgentBonusRuleVo> list = Lists.newArrayList();
        List<AgentBonusRule> agentBonusRules = list();
        for (AgentBonusRule agentBonusRule : agentBonusRules) {
            AgentBonusRuleVo agentBonusRuleVo = new AgentBonusRuleVo();
            BeanUtils.copyProperties(agentBonusRule, agentBonusRuleVo);
            agentBonusRuleVo.setOrderType(EnumUtil.getIEnum(ProfitFromEnum.class,agentBonusRule.getOrderType()));
            list.add(agentBonusRuleVo);
        }
        return list;
    }

    @Override
    public AgentBonusRule findByOrderType(int value) {
        return getOne(new QueryWrapper<AgentBonusRule>().eq("order_type",value));
    }

    @Override
    public AgentBonusRule selectByRuleName(String ruleName) {
        return getOne(new QueryWrapper<AgentBonusRule>().eq("rule_name",ruleName));
    }
}
